package com.yeu.action;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.yeu.util.ConnectionPool;

public class FastCheckInAction extends ActionSupport implements SessionAware{
	private static final long serialVersionUID = 1L;
	private Map<String,Object> session;
	private Map<String, Boolean> boolResult;
	private Map<String,Object> mapResult;
	private List<Map<String,String>> result;
	private int rows;
	private int page;
	private String sort;
	private String order;
	private String keyword;
	private String findBy;
	

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getFindBy() {
		return findBy;
	}

	public void setFindBy(String findBy) {
		this.findBy = findBy;
	}

	public List<Map<String, String>> getResult() {
		return result;
	}

	public Map<String, Boolean> getBoolResult() {
		return boolResult;
	}
	
	public Map<String, Object> getMapResult() {
		return mapResult;
	}
	
	public void setRows(int rows) {
		this.rows = rows;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session=session;
	}
	
	
/*	public String eventId(){
		String eventId=(String) session.get("eventId");
		String sql="select * from EVENT,DEPARTMENTLEADER,USERTABLE,CLASS where EVENTID=? and EVENT.EVENTADMINBY=DEPARTMENTLEADER.DEPARTMENTID and DEPARTMENTLEADER.USERID=USERTABLE.USERID and EVENT.REPORTERCLASS=CLASS.CLASSID";
		result=ConnectionPool.executeQuery(sql, eventId);
		for (Map<String, String> map : result) {
			map.remove("ROLEID");
			map.remove("PASSWORD");
			map.remove("USERID");
			map.remove("CLASSDIRECTORNAME");
			map.remove("CLASSMONITORNAME");
		}
		return SUCCESS;
	}*/
	
	public String findStudent(){
		//System.out.println("hello");
		String sql="";
		String total="";
		mapResult = new HashMap<String,Object>();
		if(findBy.equals("姓名")){
			total=ConnectionPool.executeQuery("select COUNT(*) from student where studentname like ?",keyword).get(0).get("COUNT(*)");
			sql = String.format("select T.STUDENTID, T.STUDENTNAME, T.DEPARTMENTNAME,T.CLASSNAME from (select STUDENTID,STUDENTNAME,DEPARTMENTNAME,CLASSNAME,ROWNUM RN from student where studentname like ? and ROWNUM <= %d) T where RN > %d",rows*page,rows*(page-1));
			result = ConnectionPool.executeQuery(sql,keyword);
			mapResult.put("total", total);
			mapResult.put("rows", result);
		}
		else if(findBy.equals("学号")){
			total = ConnectionPool.executeQuery("select COUNT(*) from student where studentid = ?",keyword).get(0).get("COUNT(*)");
			sql = String.format("select T.STUDENTID, T.STUDENTNAME, T.DEPARTMENTNAME,T.CLASSNAME from (select STUDENTID,STUDENTNAME,DEPARTMENTNAME,CLASSNAME,ROWNUM RN from student where studentid = ? and ROWNUM <= %d) T where RN > %d",rows*page,rows*(page-1));
			result = ConnectionPool.executeQuery(sql, keyword);
			mapResult.put("total", total);
			mapResult.put("rows", result);
		}
		return "mapResult";
	}
	
	public String findEmp(){
		String total = ConnectionPool.executeQuery("select COUNT(*) from DEPARTMENTEMP where EMPNAME like ?", keyword).get(0).get("COUNT(*)");
		String sql = String.format("select t.* from (select DEPARTMENTNAME,EMPNAME,ROWNUM RN from DEPARTMENTEMP where EMPNAME like ? and ROWNUM <=%d) t where RN > %d",rows*page,rows*(page-1));
		result = ConnectionPool.executeQuery(sql, keyword);
		mapResult = new HashMap<String,Object>();
		mapResult.put("total", total);
		mapResult.put("rows", result);
		return "mapResult";
	}
	
	public String queryStudent(){
		String sql = "select S.*, C.CLASSDIRECTORNAME,C.CLASSDIRECTORPHONE,C.CLASSMONITORNAME,R.STUROOM" +
		          " from STUDENT S,CLASS C,STUDROOM R where S.CLASSID = C.CLASSID and R.STUDID = S.STUDENTID and S.STUDENTID = ?";
		result = ConnectionPool.executeQuery(sql, keyword);
		return "success";
	}
	
	public String queryEmp(){
		String sql = "select D.EMPNAME,D.DEPARTMENTNAME,U.USERNAME,U.PHONE,T.BUILDING,T.ROOM "
				+ " from departmentemp D,usertable U,teacherroom T " 
				+ " where D.departmentid = U.departmentid " 
                + " and D.empname = T.name and U.userid like '%001' and D.empname = ?";
		result = ConnectionPool.executeQuery(sql, keyword);
		return "success";
	}
	public String userId(){
		mapResult=new HashMap<String, Object>();
		String userId=(String) session.get("userId");
		String sqlA=String.format("SELECT * FROM (SELECT A.*,ROWNUM RN FROM (select event2.* from event2,department,departmentleader where department.departmentname = event2.admindepartment and department.departmentid = departmentleader.departmentid and departmentleader.userid=? ORDER BY %s %s) A WHERE ROWNUM<=%d) WHERE RN>%d",sort,order,rows*page,rows*(page-1));
		
		//String sqlB=String.format("select t.*,rownum from event2 t where rownum <= %d and rownum >%d ",rows*page,rows*(page-1));
		String sqlB = String.format("select * from  (select t.*, rownum RN from event2 t  where rownum <= %d ) A where RN > %d", rows*page, rows*(page-1));
		if(userId.equals("bw0001")){
			result = ConnectionPool.executeQuery(sqlB);
			String total = ConnectionPool.executeQuery("select COUNT(*) from event2").get(0).get("COUNT(*)");
			mapResult.put("total", total);
			mapResult.put("rows", result);
			//System.out.println(total);
			//System.out.println(result);
			//System.out.println(page);
			//System.out.println(rows);
			return "mapResult";
		}
		else{
		//System.out.println("sort="+sort+",order="+order+","+"rows="+rows+",page="+page);
		/*for (Map<String, String> map : result) {
			map.remove("ROLEID");
			map.remove("REPORTERDEPARTMENT");
			map.remove("CLASSDEPARTMENTID");
			map.remove("DEPARTMENTID");
			map.remove("REPORTERCLASS");
			map.remove("EVENTADMINBY");
			map.remove("USERID");
			map.remove("RN");
			map.remove("CLASSID");
		}*/
		//System.out.println("records =" + result.size());
		//sql="SELECT COUNT(*) FROM EVENT WHERE EVENTADMINBY=?";
		//String total=ConnectionPool.executeQuery(sql,userId).get(0).get("COUNT(*)");
			String sqlA1 = String.format("select COUNT(*) from event2,department,departmentleader where department.departmentname = event2.admindepartment and department.departmentid = departmentleader.departmentid and departmentleader.userid=? ORDER BY %s %s",sort,order);
			result=ConnectionPool.executeQuery(sqlA,userId);
			//System.out.println(sqlA1);
			List<Map<String,String>> totalList = ConnectionPool.executeQuery(sqlA1,userId);
			//System.out.println(totalList);
			String total = totalList.get(0).get("COUNT(*)");
			mapResult.put("total", total);
			mapResult.put("rows", result);
			return "mapResult";
		}
	}

}
